﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;

public static class AnimatorExtension
{
    [CompilerGenerated]
    private static Func<AnimatorControllerParameter, int> <>f__am$cache2;
    private static Dictionary<string, int> idLookup = new Dictionary<string, int>();
    private static Dictionary<Animator, HashSet<int>> parameterIdLookup = new Dictionary<Animator, HashSet<int>>();

    private static int GetID(string name)
    {
        int num;
        if (!idLookup.TryGetValue(name, out num))
        {
            num = Animator.StringToHash(name);
            idLookup.Add(name, num);
        }
        return num;
    }

    private static HashSet<int> GetParameterIDs(this Animator anim)
    {
        HashSet<int> set;
        if (!parameterIdLookup.TryGetValue(anim, out set))
        {
            if (<>f__am$cache2 == null)
            {
                <>f__am$cache2 = x => x.nameHash;
            }
            set = new HashSet<int>(anim.parameters.Select<AnimatorControllerParameter, int>(<>f__am$cache2));
            parameterIdLookup.Add(anim, set);
        }
        return set;
    }

    public static bool HasParameter(this Animator anim, int id)
    {
        if (anim == null)
        {
            return false;
        }
        return anim.GetParameterIDs().Contains(id);
    }

    public static bool HasParameter(this Animator anim, string name)
    {
        return anim.HasParameter(GetID(name));
    }

    public static bool IsStatePlaying(this Animator anim, int shortNameHash)
    {
        for (int i = 0; i < anim.layerCount; i++)
        {
            if (anim.GetCurrentAnimatorStateInfo(i).shortNameHash == shortNameHash)
            {
                return true;
            }
        }
        return false;
    }

    public static bool IsStatePlaying(this Animator anim, string shortName)
    {
        for (int i = 0; i < anim.layerCount; i++)
        {
            if (anim.GetCurrentAnimatorStateInfo(i).IsName(shortName))
            {
                return true;
            }
        }
        return false;
    }

    public static void PlayOverrideClip(this Animator anim, string orginalClipName, AnimationClip clip)
    {
        <PlayOverrideClip>c__AnonStorey7D storeyd = new <PlayOverrideClip>c__AnonStorey7D {
            anim = anim,
            orginalClipName = orginalClipName
        };
        storeyd.anim.SetOverrideClip(storeyd.orginalClipName, clip);
        Singleton<AnimatorExtensionHelper>.Instance.StartWaitForFrames(2, new Action(storeyd.<>m__15B));
    }

    public static void SetOverrideClip(this Animator anim, string orginalClipName, AnimationClip clip)
    {
        if (anim != null)
        {
            AnimatorOverrideController runtimeAnimatorController = anim.runtimeAnimatorController as AnimatorOverrideController;
            if ((runtimeAnimatorController != null) && (runtimeAnimatorController[orginalClipName] != clip))
            {
                runtimeAnimatorController[orginalClipName] = clip;
            }
        }
    }

    public static void TrySetBool(this Animator anim, int id, bool value)
    {
        if (anim.HasParameter(id))
        {
            anim.SetBool(id, value);
        }
    }

    public static void TrySetBool(this Animator anim, string name, bool value)
    {
        int iD = GetID(name);
        if (anim.HasParameter(iD))
        {
            anim.SetBool(iD, value);
        }
    }

    public static void TrySetFloat(this Animator anim, int id, float value)
    {
        if (anim.HasParameter(id))
        {
            anim.SetFloat(id, value);
        }
    }

    public static void TrySetFloat(this Animator anim, string name, float value)
    {
        int iD = GetID(name);
        if (anim.HasParameter(iD))
        {
            anim.SetFloat(iD, value);
        }
    }

    public static void TrySetTrigger(this Animator anim, int id)
    {
        if (anim.HasParameter(id))
        {
            anim.SetTrigger(id);
        }
    }

    public static void TrySetTrigger(this Animator anim, string name)
    {
        int iD = GetID(name);
        if (anim.HasParameter(iD))
        {
            anim.SetTrigger(iD);
        }
    }

    [CompilerGenerated]
    private sealed class <PlayOverrideClip>c__AnonStorey7D
    {
        internal Animator anim;
        internal string orginalClipName;

        internal void <>m__15B()
        {
            if ((this.anim != null) && this.anim.isInitialized)
            {
                this.anim.CrossFade(this.orginalClipName, 0.2f, -1, 0f);
            }
        }
    }
}

